#ifndef __SUBSYSTEM_SAPLING_BLOCK_BEHAVIOR_BASE_H__
#define __SUBSYSTEM_SAPLING_BLOCK_BEHAVIOR_BASE_H__

#include <map>
#include <mutex>
#include "Subsystems/BlockBehaviors/SubsystemBlockBehavior.hpp"
#include "GameRandom.hpp"
#include "GameEnum.h"
#include "GameLock.hpp"


namespace PocketSurvival
{
    class SubsystemTimeOfDay;
    class SubsystemTerrain;
    class SubsystemGameInfo;
    class GameRandom;
    class BlocksManager;
    class PlantsManager;

    // 树苗行为，种树
    class SubsystemSaplingBlockBehavior : public SubsystemBlockBehavior
    {
    protected:
        struct SaplingData
        {
            Point3 point;
            TreeType type;
            bool determineGrowth;
            double matureTime;
        };

    protected:
        std::map<Point3, SaplingData> m_saplings;
        SpinLock m_saplingsLock;

    public:
        SubsystemTerrain *m_terrain;
        SubsystemTimeOfDay *m_timeOfDay;
        SubsystemGameInfo *m_gameInfo;


    public:
        SubsystemSaplingBlockBehavior();

        virtual void onNeighborBlockChanged(int32_t x, int32_t y, int32_t z, int32_t neighborX, int32_t neighborY, int32_t neighborZ) override;
        virtual void onBlockAdded(int32_t value, int32_t oldValue, int32_t x, int32_t y, int32_t z) override;
        virtual void onBlockAdded(int32_t value, int32_t oldValue, int32_t x, int32_t y, int32_t z, ComponentMiner *miner) override;
        virtual void onBlockRemoved(int32_t value, int32_t newValue, int32_t x, int32_t y, int32_t z) override;
        virtual void update(float dt) override;

        void matureSapling(SaplingData &saplingData);
        bool growTree(int32_t x, int32_t y, int32_t z, TreeType treeType);
        void addSapling(const SaplingData &saplingData);
        void removeSapling(const Point3 &point);
        
        virtual void load(const nlohmann::json &json) override;
        virtual void save(nlohmann::json& json) override;
        virtual const std::string& getName() const override;
        virtual SubUpdateType getUpdateType() override;
    };
}

#endif
